package com.example.demo.service;

import com.example.demo.entity.Commodity;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class CommodityService {
    private Map<Long, Commodity> commodityInfos = Collections.synchronizedMap(new HashMap<Long,Commodity>());

    public Commodity addCommodity(Commodity commodity){
        commodityInfos.put(commodity.getId(),commodity);
        return commodity;
    }

    public Commodity getCommodity(long id){return commodityInfos.get(id);}

    public List<Commodity> findCommodity(String commodityName){
        List<Commodity> result = new ArrayList<>();
        for(Commodity commodity:commodityInfos.values()){
            if(commodity.getName().equals(commodityName))
                result.add(commodity);
        }
        return result;
    }

    public void updateCommodity(long id,Commodity commodity){
        Commodity temp = commodityInfos.get(id);
        temp.setCustomer(commodity.getCustomer());
        temp.setSupplier(commodity.getSupplier());
        temp.setAmount(commodity.getAmount());
        temp.setName(commodity.getName());
        temp.setPurchaseDate(commodity.getPurchaseDate());
        temp.setPurchasePrice(commodity.getPurchasePrice());
        temp.setSaleDate(commodity.getSaleDate());
        temp.setSalePrice(commodity.getSalePrice());
        commodityInfos.put(id,temp);
    }

    public void deleteCommodity(long id){commodityInfos.remove(id);}

}
